#!/usr/bin/env python
import re
import urllib
class TagParser:

	def __init__(self,content=''):
		self.content=content

	def getTagByAttr(self,value,content=None,tag='div',attr='id'):
		if not content:content=self.content
		attrBegin='<\s*'+tag+'[^>]*'+attr+'[^>]*=[^>]*'+value+'[^>]*>'
		tagBegin='<\s*'+tag+'[^>]*>'
		tagEnd='<\s*/'+tag+'\s*>'
		attrBegin=re.search(attrBegin,content)
		tagBegin=re.compile(tagBegin)
		tagEnd=re.compile(tagEnd)
		index=attrBegin.end()+1

		while True:
			begin=tagBegin.search(content,index)
			end=tagEnd.search(content,index)
			index=end.end()+1
			if begin == None or begin.start() > end.start():
				return content[attrBegin.start():index]
	

	def delScript(self,content=None,tag='script'):
		if not content:content=self.content
		tagBegin=re.compile('<\s*\s*'+tag+'[^<>]*>')
		tagEnd=re.compile('<\s*/\s*'+tag+'\s*>')
		segs=[]
		index=0
		result=''
		while True:
			begin=tagBegin.search(content,index)
			end=tagEnd.search(content,index)
			if not begin:break
			index = end.end()
			segs.append((begin.start(),index))
			#print content[begin.start():index]
		for span in segs:
			#print span
			result += content[index:span[0]]
			index=span[1]
		return result


cont=urllib.urlopen('http://hi.baidu.com/yingnet/blog/item/6eba5d90b6939285a877a431.html').read()
tp=TagParser(cont)
cont=tp.delScript(cont)
print tp.getTagByAttr('m_blog',cont)
#print cont
#tp=TagParser(open('/home/xddai/share/test.html').read())
#print tp.delScript(tag='script')
